package com.spring.boot.standard.admin.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.spring.boot.standard.admin.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.spring.boot.standard.admin.common.utils.GsonUtil;
import com.spring.boot.standard.admin.dao.CodebaseRepository;
import com.spring.boot.standard.admin.entity.Codebase;
import com.spring.boot.standard.admin.service.CodebaseService;

/**
 * @author qinhao
 * @since Tue Oct 12 16:24:28 CST 2021
 */
@Service
@EnableTransactionManagement
public class CodebaseServiceImpl implements CodebaseService {
	
    @Autowired
    private CodebaseRepository codebaseRepository;

    @SuppressWarnings("serial")
	@Override
	public Page<Codebase> findAll(Pageable pageable,Object[] args) {
		Page<Codebase> result = codebaseRepository.findAll(new Specification<Codebase>() {
	        @Override
	        public Predicate toPredicate(Root<Codebase> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>(); //多条件搜索的写法
				if (!StringUtils.isEmpty(args[0]) && !StringUtils.isEmpty(args[1])) {
					// 多条件搜索的写法
					list.add(cb.and(cb.like(root.get(args[0].toString()).as(String.class),"%" + args[1] + "%")));
					if(args[2].toString().equals("my")){
						list.add(cb.equal(root.get("creator").as(String.class), (String) args[3]));
					}
					Predicate[] p = new Predicate[list.size()];
					return cb.and(list.toArray(p));
				}else{
					if(args[2].toString().equals("my")){
						return cb.equal(root.get("creator").as(String.class), (String) args[3]);
					}else {
						return null;
					}
				}
	        }
	    }, pageable);
	    return result;
	}

	@Override
	public Codebase findById(Integer id) {
		Codebase codebase = new Codebase();
		codebase.setId(id);
		Example<Codebase> example = Example.of(codebase);
		try {
			return codebaseRepository.findOne(example).get();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void deleteById(Integer id) {
		Codebase codebase =  findById(id);
		codebaseRepository.delete(codebase);
	}

	@Override
	public Codebase save(Codebase codebase) {
		return codebaseRepository.save(codebase);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int saveFromList(List<Map<String,String>> list) throws Exception {
		int i=0;
		List<Codebase> codebaselist = new ArrayList<Codebase>();
		for(Map<String,String> map:list){
			Codebase codebase = GsonUtil.getGson().fromJson(GsonUtil.getGson().toJson(map),Codebase.class);
			codebaselist.add(codebase);
		}
		for(Codebase codebase:codebaselist){
			codebaseRepository.save(codebase);
			i++;
		}
		return i;
	}

    
}
